#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2020/9/3 15:10
# @Author : 吴敬超
# explain:
import pandas as pd
import threading
import numpy as np
import netCDF4 as nc
import datetime
import csv
import time as ti
import glob
import multiprocessing
from compiler.ast import flatten

from collections import defaultdict, OrderedDict
import json
#
# xlen = 1047
# ylen = 1497

file_path_wrf = 'D:/nc2json/wrf_2020070700_need.nc'
file_path_roms = 'D:/nc2json/roms_2020070700_need.nc'

file_obj_roms = nc.Dataset(file_path_roms)
file_obj_wrf = nc.Dataset(file_path_wrf)

jsonsource = "TIME"
jsonlat = [-16.000000, 62.000000]
jsonnlat = 157
jsonlng = [39.000000, 178.000000]
jsonnlng = 279

# z_t = file_obj.variables['z_t'][:]
# OLR = file_obj_wrf.variables['OLR'][:]
# print OLR[0][0][0]

ocean_time = file_obj_roms.variables['ocean_time']
# wrfTime = file_obj_wrf.variables['Time']

ocean_times = nc.num2date(ocean_time[:], ocean_time.units)
# wrfTimes = nc.num2date(wrfTime[:], wrfTime.units)

# print ocean_times[1].strftime("%Y-%m-%d_%H:%M:%S")
# print wrfTimes[1].strftime("%Y-%m-%d_%H:%M:%S")
#
# roms_lon_u = file_obj_roms.variables['lon_u'][:]
# roms_lon_v = file_obj_roms.variables['lon_v'][:]
# roms_lat_u = file_obj_roms.variables['lat_u'][:]

#
# # 经纬   Longitude and latitude
# def rangflag(Lon=0, lat=0, Lonstart=39, Lonstop=178, latstart=-16, latstop=62):
#     if (Lonstart <= Lon <= Lonstop) and (latstart <= lat <= latstop):
#         return True
#
#     return False
#
#
# # 经纬   Longitude and latitude
# def Longituderangflag(x, xstart=39, xstop=178):
#     if xstart <= x <= xstop:
#         return True
#
#     return False
#
#
# # 经纬   Longitude and latitude
# def latituderangflag(y, ystart=-16, ystop=62):
#     if ystart <= y <= ystop:
#         return True
#
#     return False
#

#
#
romsu = file_obj_roms.variables['u'][:]
romsv = file_obj_roms.variables['v'][:]
romstemp = file_obj_roms.variables['temp'][:]
romszeta = file_obj_roms.variables['zeta'][:]
wrfRAINNC = file_obj_wrf.variables['RAINNC'][:]
wrft2 = file_obj_wrf.variables['T2'][:]
wrfolr = file_obj_wrf.variables['OLR'][:]
wrfwu = file_obj_wrf.variables['U10'][:]
wrfwv = file_obj_wrf.variables['V10'][:]


#
# print romsu.shape
# print len(romsu)
# print len(romsu[5][0])
# print type(romsu[5][0])
# print type(romsu[5][0].flatten())
#
# print len(romsu[6][0][2])
#
# print roms_lon_u[200][200]
#
# for utime in range(0, len(romsu)):
#
#     for ueta_u in range(0, len(romsu[utime][0])):
#
#         for uxi_u in range(0, len(romsu[utime][0][ueta_u])):
#             print "000000000"
#
#             # roms_lon_u[ueta_u][uxi_u]
#
#             rangflag(roms_lon_u[ueta_u][uxi_u], roms_lat_u[ueta_u][uxi_u])


#
#
# temp = file_obj_roms.variables['temp'][:]
#
#
# templat=temp[0][0][0][1430]
# print templat
#
#
#
# romsulat=romsu[0][0][0][1430]
# print romsulat
#
#
#
# def romsxy(timenum):
#     for x in range(xlen):
#         for y in range(ylen):
#             # 判读u的经纬度
#             uxyflag = rangflag(roms_lon_u[ueta_u][uxi_u], roms_lat_u[ueta_u][uxi_u])


def filechanger(threadName, timenum, ffpath="D:/nc2json/"):
    print "thread start Name is " + str(threadName)
    video = OrderedDict()

    video["source"] = jsonsource
    video["lat"] = jsonlat
    video["nlat"] = jsonnlat
    video["lng"] = jsonlng
    video["nlng"] = jsonnlng
    video["time"] = ocean_times[timenum].strftime("%Y-%m-%d_%H:%M:%S")

    video["u"] = romsu[timenum][0].flatten()
    video["v"] = romsv[timenum][0].flatten()
    video["temp"] = romstemp[timenum][0].flatten()
    video["zeta"] = romszeta[timenum].flatten()
    video["rain"] = wrfRAINNC[timenum].flatten()
    video["t2"] = wrft2[timenum].flatten()
    video["olr"] = wrfolr[timenum].flatten()
    video["wu"] = wrfwu[timenum].flatten()
    video["wv"] = wrfwv[timenum].flatten()

    # json_str = json.dumps(video)
    # print json_str
    # json_str = json.dumps(video)
    # print json_str
    # crcm1.json
    filename = ffpath + "crcm" + str(timenum) + ".json"

    with open(filename, "w") as f:
        json.dump(video, f)
    #
    # with open('videotest_datan.json', 'w') as json_file:
    #     json_file.write(json_str)

    # print ti.strftime("%Y-%m-%d %H:%M:%S", ti.localtime())
    print "thread end Name is" + str(threadName)

    print "%s: %s" % (threadName, ti.ctime(ti.time()))


if __name__ == "__main__":
    # print("main")

    print ti.strftime("%Y-%m-%d %H:%M:%S", ti.localtime())

    pool = multiprocessing.Pool(processes=8)
    for timenum in range(0, len(ocean_times), 6):
        Threadname = "process-" + str(timenum)

        pool.apply_async(filechanger, (Threadname, timenum,))
        # msg = "hello %d" % (i)
        # pool.apply_async(func, (msg,))
    pool.close()
    pool.join()
    # print "Sub-process(es) done."
    #
    # threads = []
    #
    # for timets in range(len(times)):
    #     Threadname = "Thread-" + str(timets)
    #
    #     Thread = myThread(timets, Threadname, timets)
    #     Thread.start()
    #     threads.append(Thread)
    #
    # print "等待线程全部结束"
    #
    # for t in threads:
    #     t.join()

    # print "开始生成csv头文件"
    #
    # csvpathsou = "D:/fiecsv/svalt/"
    # csvsouhe = "headsvalt.csv"
    # csvtp = "svalt*.csv"
    #
    # csvhead(csvpathsou, csvsouhe)
    # csvmerger(csvpathsou, csvtp, csvsouhe)

    print ti.strftime("%Y-%m-%d %H:%M:%S", ti.localtime())
